package com.framework.buss.ai.service.impl;

import cn.hutool.core.lang.UUID;
import com.framework.buss.ai.constant.BussConstant;
import com.framework.buss.ai.mapper.SettingMapper;
import com.framework.buss.ai.model.DiseaseType;
import com.framework.buss.ai.params.DiseaseTypeParam;
import com.framework.buss.ai.params.SettingParam;
import com.framework.buss.ai.service.IDiseaseTypeService;
import com.framework.buss.ai.service.ISettingService;
import com.framework.buss.ai.vo.SettingVO;
import com.framework.common.vo.TreeNode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangzhg
 */
@Service
public class SettingService implements ISettingService {
    @Autowired
    private SettingMapper settingMapper;
    @Autowired
    private IDiseaseTypeService diseaseTypeService;

    @Override
    public List<TreeNode<SettingVO>> tree(SettingParam param) {
        // 一级分类
        List<SettingVO> list1 = settingMapper.findDict(BussConstant.DICT_BRIDGE_TYPE);
        // 二级分类
        List<SettingVO> list2 = settingMapper.findDict(BussConstant.DICT_CATEGORY);

        List<TreeNode<SettingVO>>  ret = new ArrayList<>();

        list1.forEach(it1 -> {
            // 增加一级
            TreeNode<SettingVO> node1 = newNode(it1, 0);
            // 赋值到bridgeType
            it1.setBridgeType(it1.getCode());
            ret.add(node1);
            list2.forEach(it2 -> {
                // 第二级为菜单（字典值）
                SettingVO newVo = createNewSetting(it2);
                TreeNode<SettingVO> node2 = newNode(newVo, 1);
                newVo.setBridgeType(it1.getBridgeType());
                node1.newChildren().add(node2);
                // 第三级为按钮DiseaseType
                DiseaseTypeParam diseaseTypeParam = new DiseaseTypeParam();
                diseaseTypeParam.setBridgeType(it1.getBridgeType());
                diseaseTypeParam.setCategory(newVo.getCategory());
                List<DiseaseType> list3 = diseaseTypeService.list(diseaseTypeParam);
                setChildren(list3, node2, "0", 2);
            });
        });
        return ret;
    }

    private void setChildren(List<DiseaseType> list, TreeNode<SettingVO> node, String pid, int level) {
        List<DiseaseType> parentList = list.stream().filter(it -> pid.equals(it.getPid())).collect(Collectors.toList());
        list.removeAll(parentList);
        parentList.forEach(it -> {
            TreeNode<SettingVO> node3 = newNode(it, node, level);
            node.newChildren().add(node3);
            setChildren(list, node3, it.getId(), level+1);
        });
    }

    private SettingVO createNewSetting(SettingVO from) {
        SettingVO to = new SettingVO();
        BeanUtils.copyProperties(from, to);
        to.setId(from.getId());
        to.setCategory(from.getCode());
        return to;
    }

    private TreeNode<SettingVO> newNode(SettingVO it, int level) {
        TreeNode<SettingVO> node = new TreeNode<>();
        node.setId(UUID.fastUUID().toString().replaceAll("-",""));
        node.setName(it.getName());
        node.setData(it);
        node.setLevel(level);
        return node;
    }

    private TreeNode<SettingVO> newNode(DiseaseType it, TreeNode<SettingVO> pNode, int level) {
        TreeNode<SettingVO> node = new TreeNode<>();
        node.setId(UUID.fastUUID().toString().replaceAll("-",""));
        node.setName(it.getName());
        SettingVO settingVO = new SettingVO();
        settingVO.setBridgeType(it.getBridgeType());
        settingVO.setCategory(pNode.getData().getCategory());
        settingVO.setType(it.getType());
        settingVO.setId(it.getId());
        settingVO.setName(it.getName());
        node.setData(settingVO);
        node.setLevel(level);
        return node;
    }
}
